<template>
  <el-dialog
    v-model="dialogVisible"
    title="患者详情"
    width="800px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div v-if="patient" class="patient-detail">
      <!-- 基本信息 -->
      <el-card class="info-card">
        <template #header>
          <div class="card-header">
            <span>基本信息</span>
            <el-button type="primary" size="small" @click="handleEdit">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
          </div>
        </template>
        
        <div class="patient-info">
          <div class="avatar-section">
            <el-avatar :size="80" :src="patient.avatar">
              <el-icon size="40"><User /></el-icon>
            </el-avatar>
            <div class="basic-info">
              <h3>{{ patient.name }}</h3>
              <div class="info-tags">
                <el-tag :type="patient.gender === 'male' ? 'primary' : 'danger'" size="small">
                  {{ patient.gender === 'male' ? '男' : '女' }}
                </el-tag>
                <el-tag type="info" size="small">{{ patient.age }}岁</el-tag>
                <el-tag v-if="patient.bloodType" type="warning" size="small">
                  {{ patient.bloodType }}型血
                </el-tag>
              </div>
            </div>
          </div>
          
          <el-row :gutter="20" class="detail-rows">
            <el-col :span="12">
              <div class="info-item">
                <label>手机号：</label>
                <span>{{ patient.phone || '-' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>身份证号：</label>
                <span>{{ patient.idCard || '-' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>邮箱：</label>
                <span>{{ patient.email || '-' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>职业：</label>
                <span>{{ patient.occupation || '-' }}</span>
              </div>
            </el-col>
            <el-col :span="24">
              <div class="info-item">
                <label>地址：</label>
                <span>{{ patient.address || '-' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>婚姻状况：</label>
                <span>{{ getMaritalStatusText(patient.maritalStatus) }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="info-item">
                <label>出生日期：</label>
                <span>{{ formatDate(patient.birthDate) }}</span>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-card>
      
      <!-- 紧急联系人 -->
      <el-card class="info-card">
        <template #header>
          <span>紧急联系人</span>
        </template>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="info-item">
              <label>联系人：</label>
              <span>{{ patient.emergencyContact || '-' }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <label>联系电话：</label>
              <span>{{ patient.emergencyPhone || '-' }}</span>
            </div>
          </el-col>
        </el-row>
      </el-card>
      
      <!-- 医疗信息 -->
      <el-card class="info-card">
        <template #header>
          <span>医疗信息</span>
        </template>
        
        <div class="medical-info">
          <div class="info-item">
            <label>过敏史：</label>
            <div class="info-content">
              {{ patient.allergies || '无' }}
            </div>
          </div>
          
          <div class="info-item">
            <label>既往病史：</label>
            <div class="info-content">
              {{ patient.medicalHistory || '无' }}
            </div>
          </div>
          
          <div class="info-item">
            <label>家族病史：</label>
            <div class="info-content">
              {{ patient.familyHistory || '无' }}
            </div>
          </div>
        </div>
      </el-card>
      
      <!-- 体检记录 -->
      <el-card class="info-card">
        <template #header>
          <div class="card-header">
            <span>体检记录</span>
            <el-button type="success" size="small" @click="handleCreateAppointment">
              <el-icon><Plus /></el-icon>
              新建预约
            </el-button>
          </div>
        </template>
        
        <div v-if="checkupRecords.length === 0" class="empty-state">
          <el-empty description="暂无体检记录" :image-size="80" />
        </div>
        
        <div v-else class="checkup-timeline">
          <el-timeline>
            <el-timeline-item
              v-for="record in checkupRecords"
              :key="record.id"
              :timestamp="formatDateTime(record.checkupDate)"
              placement="top"
            >
              <el-card class="timeline-card">
                <div class="record-header">
                  <h4>{{ record.packageName }}</h4>
                  <el-tag :type="getStatusType(record.status)" size="small">
                    {{ getStatusText(record.status) }}
                  </el-tag>
                </div>
                <div class="record-content">
                  <p><strong>体检医生：</strong>{{ record.doctor || '-' }}</p>
                  <p><strong>体检结果：</strong>{{ record.result || '待出结果' }}</p>
                  <p v-if="record.remark"><strong>备注：</strong>{{ record.remark }}</p>
                </div>
                <div class="record-actions">
                  <el-button type="primary" size="small" text>
                    查看详情
                  </el-button>
                  <el-button v-if="record.reportUrl" type="success" size="small" text>
                    下载报告
                  </el-button>
                </div>
              </el-card>
            </el-timeline-item>
          </el-timeline>
        </div>
      </el-card>
      
      <!-- 备注信息 -->
      <el-card v-if="patient.remark" class="info-card">
        <template #header>
          <span>备注信息</span>
        </template>
        
        <div class="remark-content">
          {{ patient.remark }}
        </div>
      </el-card>
      
      <!-- 系统信息 -->
      <el-card class="info-card">
        <template #header>
          <span>系统信息</span>
        </template>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="info-item">
              <label>创建时间：</label>
              <span>{{ formatDateTime(patient.createdAt) }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <label>最后更新：</label>
              <span>{{ formatDateTime(patient.updatedAt) }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <label>最后体检：</label>
              <span>{{ formatDate(patient.lastCheckupDate) || '从未体检' }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <label>患者ID：</label>
              <span>{{ patient.id }}</span>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="warning" @click="handleEdit">
          <el-icon><Edit /></el-icon>
          编辑患者
        </el-button>
        <el-button type="success" @click="handleCreateAppointment">
          <el-icon><Calendar /></el-icon>
          创建预约
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

// Props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  patient: {
    type: Object,
    default: null
  }
})

// Emits
const emit = defineEmits(['update:visible', 'edit', 'create-appointment'])

// 响应式数据
const checkupRecords = ref([
  {
    id: 1,
    packageName: '基础体检套餐',
    checkupDate: '2024-01-15 09:00:00',
    doctor: '张医生',
    status: 'completed',
    result: '各项指标正常',
    remark: '建议定期复查',
    reportUrl: '/reports/1.pdf'
  },
  {
    id: 2,
    packageName: '全面体检套餐',
    checkupDate: '2023-12-10 14:30:00',
    doctor: '李医生',
    status: 'completed',
    result: '血压偏高，需要注意饮食',
    remark: '建议低盐饮食，适量运动',
    reportUrl: '/reports/2.pdf'
  }
])

// 计算属性
const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

// 方法
const handleClose = () => {
  dialogVisible.value = false
}

const handleEdit = () => {
  emit('edit', props.patient)
  handleClose()
}

const handleCreateAppointment = () => {
  emit('create-appointment', props.patient)
  ElMessage.info(`为患者 ${props.patient?.name} 创建预约功能待实现`)
}

const getMaritalStatusText = (status) => {
  const statusMap = {
    single: '未婚',
    married: '已婚',
    divorced: '离异',
    widowed: '丧偶'
  }
  return statusMap[status] || '-'
}

const getStatusType = (status) => {
  const statusMap = {
    pending: 'warning',
    confirmed: 'primary',
    in_progress: 'primary',
    completed: 'success',
    cancelled: 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    pending: '待确认',
    confirmed: '已确认',
    in_progress: '进行中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知'
}

const formatDate = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString('zh-CN')
}

const formatDateTime = (dateTime) => {
  if (!dateTime) return ''
  const date = new Date(dateTime)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<style scoped>
.patient-detail {
  max-height: 70vh;
  overflow-y: auto;
}

.info-card {
  margin-bottom: 20px;
}

.info-card:last-child {
  margin-bottom: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.patient-info {
  padding: 0;
}

.avatar-section {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
}

.basic-info h3 {
  margin: 0 0 8px 0;
  color: #2c3e50;
  font-size: 20px;
  font-weight: 600;
}

.info-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.detail-rows {
  margin-top: 0;
}

.info-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 12px;
  line-height: 1.5;
}

.info-item label {
  font-weight: 500;
  color: #606266;
  min-width: 80px;
  margin-right: 8px;
}

.info-item span {
  color: #2c3e50;
  flex: 1;
  word-break: break-all;
}

.medical-info .info-item {
  flex-direction: column;
  align-items: flex-start;
  margin-bottom: 16px;
}

.medical-info .info-item label {
  margin-bottom: 4px;
}

.info-content {
  background: #f8f9fa;
  padding: 12px;
  border-radius: 6px;
  color: #2c3e50;
  line-height: 1.6;
  width: 100%;
  min-height: 40px;
}

.empty-state {
  text-align: center;
  padding: 40px 0;
}

.checkup-timeline {
  padding: 0 20px;
}

.timeline-card {
  margin-bottom: 0;
}

.timeline-card :deep(.el-card__body) {
  padding: 16px;
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.record-header h4 {
  margin: 0;
  color: #2c3e50;
  font-size: 16px;
}

.record-content {
  margin-bottom: 12px;
}

.record-content p {
  margin: 4px 0;
  color: #606266;
  font-size: 14px;
}

.record-actions {
  display: flex;
  gap: 8px;
}

.remark-content {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  color: #2c3e50;
  line-height: 1.6;
  border-left: 4px solid #409EFF;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-timeline-item__timestamp) {
  color: #909399;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  :deep(.el-dialog) {
    width: 95% !important;
    margin: 5vh auto;
  }
  
  .avatar-section {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
  
  .basic-info {
    text-align: center;
  }
  
  .info-tags {
    justify-content: center;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .info-item label {
    min-width: auto;
    margin-right: 0;
    margin-bottom: 4px;
  }
  
  .record-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .dialog-footer {
    flex-direction: column;
  }
  
  .dialog-footer .el-button {
    width: 100%;
  }
}

@media (max-width: 480px) {
  :deep(.el-dialog) {
    width: 100% !important;
    margin: 0;
    height: 100vh;
    border-radius: 0;
  }
  
  :deep(.el-dialog__header) {
    padding: 15px 20px;
  }
  
  :deep(.el-dialog__body) {
    padding: 10px 20px;
    height: calc(100vh - 120px);
    overflow-y: auto;
  }
  
  :deep(.el-dialog__footer) {
    padding: 15px 20px;
  }
  
  .checkup-timeline {
    padding: 0 10px;
  }
}
</style>